

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Evaluation {
    /**
     * constant for addition symbol
     */
    private final String ADD = "+";
    /**
     * constant for subtraction symbol
     */
    private final String SUBTRACT = "-";
    /**
     * constant for multiplication symbol
     */
    private final String MULTIPLY = "*";
    /**
     * constant for division symbol
     */
    private final String DIVIDE = "/";
    /**
     * the stack
     */
    private Stack<String> stack;

    private int num1 = 0, num2 = 0;

    public Evaluation() {
        stack = new Stack<String>();
    }

    //判断是整数还是分数并将整数分数化
    public Object judge(String s) {
        List<Integer> list = new ArrayList<Integer>();
        if (s.contains("/")) {
            Scanner scanner = new Scanner(s);
            scanner.useDelimiter("/");
            String n = scanner.next();
            num1 = new Integer(n);
            list.add(num1);
            if (scanner.hasNext()) {
                String m = scanner.next();
                num2 = new Integer(m);
                list.add(num2);
            }
        } else {
            num1 = new Integer(s);
            num2 = 1;
            list.add(num1);
            list.add(num2);
        }
        return list;
    }

    public String calculate(String expr) {
        String token, op1, op2, result = " ";
        StringTokenizer tokenizer = new StringTokenizer(expr);
        while (tokenizer.hasMoreTokens()) {
            token = tokenizer.nextToken();
            if (isOperator(token)) {
                op1 = stack.pop();
                op2 = stack.pop();
                result = evalSingleOp(token, op2, op1);
                stack.push(result);
            } else {
                stack.push(token);
            }
        }
        return result;
    }

    private boolean isOperator(String token) {
        return (token.equals("+") || token.equals("-") ||
                token.equals("*") || token.equals("/"));
    }

    private String evalSingleOp(String operation, String op1, String op2) {
        RationalNumber result = null;
        int n1, n2, n3, n4;
        List<Integer> list1 = new ArrayList<Integer>();
        List<Integer> list2 = new ArrayList<Integer>();
        list1 = (List<Integer>) judge(op1);
        list2 = (List<Integer>) judge(op2);
        n1 = list1.get(0);
        n2 = list1.get(1);
        n3 = list2.get(0);
        n4 = list2.get(1);
        RationalNumber r1 = new RationalNumber(n1, n2);
        RationalNumber r2 = new RationalNumber(n3, n4);
        switch (operation) {
            case ADD:
                result = r1.add(r2);
                break;
            case SUBTRACT:
                result = r1.subtract(r2);
                break;
            case MULTIPLY:
                result = r1.multiply(r2);
                break;
            case DIVIDE:
                result = r1.divide(r2);
        }
        String s = result.toString();
        return s;
    }

    //
    public boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }
}

